package postgis;

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import postgis.util.PostGISUtil;

import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.util.Assert;

public class PostGISUtiltest {

	@Test
	public void testGetPolygon() {
		// Polígono completo
		PointWrap point = null;
		String polygonString = "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))";
		List<PointWrap> pointsIn = new ArrayList<PointWrap>();
		point = new PointWrap(0., 0.);
		pointsIn.add(point);
		point = new PointWrap(4., 0.);
		pointsIn.add(point);
		point = new PointWrap(4., 4.);
		pointsIn.add(point);
		point = new PointWrap(0., 4.);
		pointsIn.add(point);
		point = new PointWrap(0., 0.);
		pointsIn.add(point);
		List<PointWrap> pointsOut = new ArrayList<PointWrap>();
		point = new PointWrap(1., 1.);
		pointsOut.add(point);
		point = new PointWrap(2., 1.);
		pointsOut.add(point);
		point = new PointWrap(2., 2.);
		pointsOut.add(point);
		point = new PointWrap(1., 2.);
		pointsOut.add(point);
		point = new PointWrap(1., 1.);
		pointsOut.add(point);
		Polygon polygonPostGISUtil = null;
		Polygon polygonTest = null;
		WKTReader fromText = new WKTReader();
		try {
			polygonPostGISUtil = PostGISUtil.getPolygon(pointsIn, pointsOut);
			polygonTest = (Polygon) fromText.read(polygonString);
		} catch (ParseException e) {
			fail("ParseException");
		}
		Assert.equals(polygonPostGISUtil.toText(), polygonTest.toText());
	}

	@Test
	public void testGetPointPointWrap() {
		String pointString = "POINT(0 2)";
		WKTReader fromText = new WKTReader();
		PointWrap pointWrap = new PointWrap(0., 2.);
		Point pointTest = null;
		Point pointUtil = null;
		try {
			pointTest = (Point) fromText.read(pointString);
		} catch(ParseException e) {
			fail("ParseException in Test");
		}	
		try {
			pointUtil = PostGISUtil.getPoint(pointWrap);
		} catch(ParseException e) {
			fail("ParseException in Util");
		}
		Assert.equals(pointTest.toText(), pointUtil.toText());
	}

	@Test
	public void testGetPointDoubleDouble() {
		String pointString = "POINT(0 2)";
		WKTReader fromText = new WKTReader();
		Point pointTest = null;
		Point pointUtil = null;
		try {
			pointTest = (Point) fromText.read(pointString);
		} catch(ParseException e) {
			fail("ParseException in Test");
		}	
		try {
			pointUtil = PostGISUtil.getPoint(0., 2.);
		} catch(ParseException e) {
			fail("ParseException in Util");
		}
		Assert.equals(pointTest.toText(), pointUtil.toText());
	}

	@Test
	public void testGetDistanceWithin() {
		fail("Not yet implemented");
	}

}
